package com.yhthu.texthandler;

import com.yhthu.bean.Instruction;

import java.util.ArrayList;
import java.util.List;

public class InstructionHandler extends BaseHandler<Instruction> {

    @Override
    public List<Instruction> getValidObjectFromTextContent(String content) {
        List<Instruction> validInstructions = new ArrayList<>();
        String[] instructions = super.executeLineSplitStrategy(content);
        int previousIndex = -1;
        for (String lineInstruction : instructions) {
            lineInstruction = lineInstruction.replaceFirst("\\n", "");
            String firstWord = super.executeWordSplitStrategy(lineInstruction)[0];
            if (Instruction.commandList.contains(firstWord)) {
                Instruction instruction = new Instruction(firstWord, lineInstruction.substring(firstWord.length()).trim());
                validInstructions.add(instruction);
                previousIndex++;
            } else {
                Instruction instruction = validInstructions.get(previousIndex);
                instruction.setParameters(instruction.getParameters().concat(lineInstruction));
            }
        }
        return validInstructions;
    }
}
